package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Stream;

@Service
@Transactional(readOnly = true)
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealDao setmealDao;


    @Transactional(readOnly = false)
    @Override
    public Result add(Setmeal setmeal,Integer[] checkGroupIds) {
        try{
            setmealDao.add(setmeal);
            Stream.of(checkGroupIds).forEach(checkGroupId->setmealDao.addCheckGroupToSetmeal(setmeal.getId(),checkGroupId));
            return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return  new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    @Transactional(readOnly = false)
    @Override
    public Result delete(Integer setmealId) {
        try{
            setmealDao.deleteCheckGroupToSetmeal(setmealId);
            setmealDao.delete(setmealId);
            return new Result(true,MessageConstant.DELETE_SETMEAL_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return  new Result(false,MessageConstant.DELETE_SETMEAL_FAIL);
        }

    }

    @Transactional(readOnly = false)
    @Override
    public Result update(Setmeal setmeal,Integer[] checkGroupIds) {

        try{
            setmealDao.update(setmeal);
            setmealDao.deleteCheckGroupToSetmeal(setmeal.getId());
            Stream.of(checkGroupIds).forEach(checkGroupId->setmealDao.addCheckGroupToSetmeal(setmeal.getId(),checkGroupId));
            return new Result(true,MessageConstant.EDT_SETMEAL_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return  new Result(false,MessageConstant.EDT_SETMEAL_FAIL);
        }

    }

    @Override
    public Result queryById(Integer setmealId) {
        try{
            Setmeal setmeal = setmealDao.queryById(setmealId);

            return new Result(true,MessageConstant.QUERY_SETMEALLIST_SUCCESS,setmeal);

        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEALLIST_FAIL);
        }
    }

    @Override
    public Result queryPage(QueryPageBean queryPageBean) {

        try{
            Page<Setmeal> list = PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
            setmealDao.queryAllByQueryString(queryPageBean.getQueryString());
            PageResult pageResult = new PageResult(list.getTotal(), list.getResult());
            return new Result(true,MessageConstant.QUERY_SETMEALLIST_SUCCESS,pageResult);

        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEALLIST_FAIL);
        }


    }

    @Override
    public Result queryCheckGroupIds(Integer setmealId) {
        try{
            List<Integer> checkGroupIds=setmealDao.queryCheckGroupIds(setmealId);
            return new Result(true,MessageConstant.QUERY_SETMEALLIST_SUCCESS,checkGroupIds);
        }catch (Exception e){

            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEALLIST_FAIL);
        }



    }
}
